/******************************************************
  File: src/msgcodes.c
  Description: Audio Cup Get Keypress Values ( object )
  Author: Brant Anderson
  Date: 10/30/01
  Last Update: 10/30/01
  Status: Super Crap
  To Do: -Make the keySignal Events do stuff
 ******************************************************/



#include <stdio.h>
#include "../include/keypress.h"


int setKeypress( int numKeys, char *keyPress ) {

	char tmp[numKeys];
	sprintf( tmp, "%s", keyPress );

	/* Put Keypress Values Into a Readable Variable Name */
	if( numKeys == 12 ) {

		keyUp = tmp[0];
		keyLeft = tmp[1];
		keyDown = tmp[2];
		keyRight = tmp[3];
		keyMenu = tmp[4];
		keyAction = tmp[5];
		keyDisplay = tmp[6];
		keyMode = tmp[7];
		keyList1 = tmp[8];
		keyList2 = tmp[9];
		keyList3 = tmp[10];
		keyList4 = tmp[11];

		printf( "\nKeypress Listing\n----------------\n" );
		printf( "keyUp:      %c\n", keyUp );
		printf( "keyLeft:    %c\n", keyLeft );
		printf( "keyDown:    %c\n", keyDown );
		printf( "keyRignt:   %c\n", keyRight );
		printf( "keyMenu:    %c\n", keyMenu );
		printf( "keyAction:  %c\n", keyAction );
		printf( "keyDisplay: %c\n", keyDisplay );
		printf( "keyMode:    %c\n", keyMode );
		printf( "keyList1:   %c\n", keyList1 );
		printf( "keyList2:   %c\n", keyList2 );
		printf( "keyList3:   %c\n", keyList3 );
		printf( "keyList4:   %c\n", keyList4 );

	}
	printf("\n");
	return 1;
}

SongInfo keySignal( int sock, SongInfo songinf ) {

	char buf[8192];


	sock_recv_string( sock, buf, 8192 );         /* check buffer for keypress */
	if( buf[4] == keyMenu ) {                    /**  CLEAR REPEAT KEYPRESS  **/
		sock_send_string( sock, "noop\n" );
		while( buf[0] == 'k' ) {
			sock_recv_string( sock, buf, 8192 ); /* check buffer for keypress */
		}
		songinf = menu_signal_connect( songinf );
	} else if( buf[4] == keyUp ) {
		sock_send_string( sock, "noop\n" );
		while( buf[0] == 'k' ) {
			sock_recv_string( sock, buf, 8192 ); /* check buffer for keypress */
		}
		songinf = up_signal_connect( songinf );
	} else if( buf[4] == keyList1 ) {
		sock_send_string( sock, "noop\n" );
		while( buf[0] == 'k' ) {
			sock_recv_string( sock, buf, 8192 ); /* check buffer for keypress */
		}
		songinf = listone_signal_connect( songinf );
	} else if( buf[4] == keyList2 ) {
		sock_send_string( sock, "noop\n" );
		while( buf[0] == 'k' ) {
			sock_recv_string( sock, buf, 8192 ); /* check buffer for keypress */
		}
		songinf = listtwo_signal_connect( songinf );
	} else if( buf[4] == keyLeft ) {
		sock_send_string( sock, "noop\n" );
		while( buf[0] == 'k' ) {
			sock_recv_string( sock, buf, 8192 ); /* check buffer for keypress */
		}
		songinf = left_signal_connect( songinf );
	} else if( buf[4] == keyAction ) {
		sock_send_string( sock, "noop\n" );
		while( buf[0] == 'k' ) {
			sock_recv_string( sock, buf, 8192 ); /* check buffer for keypress */
		}
		menuDisplay( action_signal_connect( songinf ) );
	} else if( buf[4] == keyRight ) {
		sock_send_string( sock, "noop\n" );
		while( buf[0] == 'k' ) {
			sock_recv_string( sock, buf, 8192 ); /* check buffer for keypress */
		}
		songinf = right_signal_connect( songinf );
	} else if( buf[4] == keyList3 ) {
		sock_send_string( sock, "noop\n" );
		while( buf[0] == 'k' ) {
			sock_recv_string( sock, buf, 8192 ); /* check buffer for keypress */
		}
		songinf = listthree_signal_connect( songinf );
	} else if( buf[4] == keyDisplay ) {
		sock_send_string( sock, "noop\n" );
		while( buf[0] == 'k' ) {
			sock_recv_string( sock, buf, 8192 ); /* check buffer for keypress */
		}
		return display_signal_connect( songinf );
	} else if( buf[4] == keyDown ) {
		sock_send_string( sock, "noop\n" );
		while( buf[0] == 'k' ) {
			sock_recv_string( sock, buf, 8192 ); /* check buffer for keypress */
		}
		songinf = down_signal_connect( songinf );
	} else if( buf[4] == keyMode ) {
		sock_send_string( sock, "noop\n" );
		while( buf[0] == 'k' ) {
			sock_recv_string( sock, buf, 8192 ); /* check buffer for keypress */
		}
		songinf = mode_signal_connect( songinf );
	} else if( buf[4] == keyList4 ) {
		sock_send_string( sock, "noop\n" );
		while( buf[0] == 'k' ) {
			sock_recv_string( sock, buf, 8192 ); /* check buffer for keypress */
		}
		songinf = listfour_signal_connect( songinf );
	}
	return songinf;
}


SongInfo up_signal_connect( SongInfo songinf ) {
    int tsn;

    if( songinf.songnum < getCount( songinf.playlist )-10 ) {
        tsn = songinf.songnum + 10;
    } else {
        tsn = getCount( songinf.playlist );
    }

    stopSong( songinf.ctype );

    songinf.filename = getSong( songinf.playlist, tsn );

    songinf = getSongInfo( songinf );
    songinf.songnum = tsn;
    songinf.loop = 1;

    menuDisplay( songinf );
    playSong( songinf.ctype );

    return songinf;
}

SongInfo left_signal_connect( SongInfo songinf ) {
    int tsn;

    if( songinf.songnum > 0 ) {
        tsn = songinf.songnum - 1;
    } else if( songinf.songnum == 0 ) {
        tsn = getCount( songinf.playlist );
    } else {
        tsn = 0;
    }

    stopSong( songinf.ctype );
    songinf.filename = getSong( songinf.playlist, tsn );

/** Can I make these steps into a single function with
    songinf = getSongInfo( songinf ); ???
    In the getSongInfo function make it change all the
    data in the SongInfo struct. **/

    songinf = getSongInfo( songinf );
    songinf.songnum = tsn;
    songinf.loop = 1;

    menuDisplay( songinf );
    playSong( songinf.ctype );

    return songinf;
}

SongInfo down_signal_connect( SongInfo songinf ) {
    int tsn;

    if( songinf.songnum > 10 ) {
        tsn = songinf.songnum - 10;
    } else {
        tsn = 0;
    }

    stopSong( songinf.ctype );

    songinf.filename = getSong( songinf.playlist, tsn );

    songinf = getSongInfo( songinf );
    songinf.songnum = tsn;
    songinf.loop = 1;

    menuDisplay( songinf );
    playSong( songinf.ctype );

    return songinf;
}

SongInfo right_signal_connect( SongInfo songinf ) {
    int tsn;

    if( songinf.songnum < getCount( songinf.playlist ) ) {
        tsn = songinf.songnum + 1;
    } else {
        tsn = 0;
    }

    stopSong( songinf.ctype );

    songinf.filename = getSong( songinf.playlist, tsn );

    songinf = getSongInfo( songinf );
    songinf.songnum = tsn;
    songinf.loop = 1;

    menuDisplay( songinf );
    playSong( songinf.ctype );

    return songinf;
}

SongInfo action_signal_connect( SongInfo songinf ) {
    if(songinf.scroller == 0) {
    	songinf.scroller = 1;
	} else {
		songinf.scroller = 0;
	}

    return songinf;
}

SongInfo display_signal_connect( SongInfo songinf ) {
    songinf.loop = 0;
    return songinf;
} /* Temperary Loop Killer */

SongInfo menu_signal_connect( SongInfo songinf ) {
	songinf.playlist = "/home/rabrant/.acup/playlist/menu.m3u";

	stopSong( songinf.ctype );
    songinf.filename = getSong( songinf.playlist, 0 );
	printf( "Song Playing: %s\n", songinf.filename );


	songinf = getSongInfo( songinf );
	songinf.songnum = 0;
	songinf.loop = 1;

	menuDisplay( songinf );
	playSong( songinf.ctype );

	return songinf;
}

SongInfo mode_signal_connect( SongInfo songinf ) {
	songinf.playlist = "/home/rabrant/.acup/playlist/mode.m3u";

	stopSong( songinf.ctype );
    songinf.filename = getSong( songinf.playlist, 0 );
	printf( "Song Playing: %s\n", songinf.filename );


	songinf = getSongInfo( songinf );
	songinf.songnum = 0;
	songinf.loop = 1;

	menuDisplay( songinf );
	playSong( songinf.ctype );

	return songinf;
}

SongInfo listone_signal_connect( SongInfo songinf ) {
	songinf.playlist = "/home/rabrant/.acup/playlist/listone.m3u";

	stopSong( songinf.ctype );
    songinf.filename = getSong( songinf.playlist, 0 );
	printf( "Song Playing: %s\n", songinf.filename );


	songinf = getSongInfo( songinf );
	songinf.songnum = 0;
	songinf.loop = 1;

	menuDisplay( songinf );
	playSong( songinf.ctype );

	return songinf;
}

SongInfo listtwo_signal_connect( SongInfo songinf ) {
	songinf.playlist = "/home/rabrant/.acup/playlist/listtwo.m3u";

	stopSong( songinf.ctype );
    songinf.filename = getSong( songinf.playlist, 0 );
	printf( "Song Playing: %s\n", songinf.filename );


	songinf = getSongInfo( songinf );
	songinf.songnum = 0;
	songinf.loop = 1;

	menuDisplay( songinf );
	playSong( songinf.ctype );

	return songinf;
}

SongInfo listthree_signal_connect( SongInfo songinf ) {
	songinf.playlist = "/home/rabrant/.acup/playlist/listthree.m3u";

	stopSong( songinf.ctype );
    songinf.filename = getSong( songinf.playlist, 0 );
	printf( "Song Playing: %s\n", songinf.filename );


	songinf = getSongInfo( songinf );
	songinf.songnum = 0;
	songinf.loop = 1;

	menuDisplay( songinf );
	playSong( songinf.ctype );

	return songinf;
}

SongInfo listfour_signal_connect( SongInfo songinf ) {
	songinf.playlist = "/home/rabrant/.acup/playlist/listfour.m3u";

	stopSong( songinf.ctype );
    songinf.filename = getSong( songinf.playlist, 0 );
	printf( "Song Playing: %s\n", songinf.filename );


	songinf = getSongInfo( songinf );
	songinf.songnum = 0;
	songinf.loop = 1;

	menuDisplay( songinf );
	playSong( songinf.ctype );

	return songinf;
}
